//
//  ZYHeaderImgCell.m
//  Project_B
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "ZYHeaderImgCell.h"


@interface ZYHeaderImgCell ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *showScrollV;/** 轮播图*/
@property(nonatomic,strong)UIPageControl *page;/** 小点*/
@property(nonatomic,strong)UILabel *titleLb;/** 标题*/
@property(nonatomic,strong)NSMutableArray *picArr;
@property(nonatomic,strong)NSMutableArray *titleArr;
@property(nonatomic,strong)NSMutableArray *urlIdArr;

@end
@implementation ZYHeaderImgCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}



#pragma mark - 子视图
-(void)createSubView{
    
    
    
    
    //轮播图
    self.showScrollV = [[UIScrollView alloc]init];
//    self.showScrollV.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.showScrollV];
    self.showScrollV.pagingEnabled = YES;
    self.showScrollV.bounces = NO;
    self.showScrollV.delegate = self;
    self.showScrollV.showsHorizontalScrollIndicator = NO;
    
    [_showScrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.top.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(SCREEN_HEIGHT/3.5);
    }];
    
   
    
    //小点
    self.page = [[UIPageControl alloc]init];
    self.page.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.page];
    
    self.page.currentPage = 0;    //设置初始小点位置
        self.page.pageIndicatorTintColor = [UIColor blackColor];//设置未选中点得颜色
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];//设置选中小点颜色
    self.page.alpha = 0.7;
    
    
    [_page mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-SPACE/2);
        make.bottom.mas_equalTo(-SPACE/2);
        make.width.mas_equalTo(SCREEN_WIDTH/10);
        make.height.mas_equalTo(SCREEN_WIDTH/20);
    }];
    
    //方法
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
    //标题
    self.titleLb = [[UILabel alloc]init];
//    self.titleLb.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:self.titleLb];
    self.titleLb.textAlignment = NSTextAlignmentLeft;
    self.titleLb.font = [UIFont boldSystemFontOfSize:15*ADAPT_FONT];
    self.titleLb.textColor = [UIColor whiteColor];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(SPACE);
        make.bottom.mas_equalTo(-SPACE/2);
        make.width.mas_equalTo(SCREEN_WIDTH/4*3 - SPACE*1.5);
        make.height.mas_equalTo(SCREEN_HEIGHT/3.5 - SCREEN_HEIGHT/4);
        
    }];
    
    
    //计时器方法
    //    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    
}


#pragma mark - 计时器方法
-(void)timerAction{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.showScrollV setContentOffset:CGPointMake(self.showScrollV.contentOffset.x + SCREEN_WIDTH, 0) animated:YES];
        
    }];
    
    if(self.showScrollV.contentOffset.x == SCREEN_WIDTH*self.picArr.count){
        self.showScrollV.contentOffset = CGPointMake(0, 0);
    }
    self.page.currentPage = self.showScrollV.contentOffset.x/SCREEN_WIDTH;
    
}

#pragma mark - 页码方法
-(void)pageAction:(UIPageControl *)page{
    
    [self.showScrollV setContentOffset:CGPointMake(SCREEN_WIDTH * page.currentPage + 1, 0) animated:YES];
    
}


#pragma mark - 滑动停止时触发方法(轮播方法)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    
    self.page.currentPage = self.showScrollV.contentOffset.x/SCREEN_WIDTH ;
 
    
    self.titleLb.text = [self.titleArr objectAtIndex:self.page.currentPage];
    
}




#pragma mark -
-(void)setModel:(NewestModel *)model{
    _model = model;
    

    if (model.ads.count == 0) {
        
        self.showScrollV.contentSize = self.showScrollV.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT/3.5);
        
        UIImageView *headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3.5)];
        
        [self.showScrollV addSubview:headerImgView];
        
        [headerImgView sd_setImageWithURL:model.imgsrc placeholderImage:PLACEHOLDER_IMAGE];
        self.titleLb.text = model.title;
    }else{
    
        self.titleLb.text = [_titleArr objectAtIndex:0];

    
    self.picArr = [NSMutableArray array];
    self.titleArr = [NSMutableArray array];
    self.urlIdArr = [NSMutableArray array];


    
    for (NSMutableDictionary *dic in model.ads) {
        
        NSString *url = [dic objectForKey:@"imgsrc"];
        NSString *title = [dic objectForKey:@"title"];
        NSString *urlId = [dic objectForKey:@"url"];
        
        [self.picArr addObject:url];
        [self.titleArr addObject:title];
        [self.urlIdArr addObject:urlId];
    }
    
   
    
    
    if (self.picArr.count <= 1) {
        self.page.numberOfPages = 0;
    }else{
        self.page.numberOfPages = self.picArr.count + 1; //设置点数
    }

    
    
   
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.picArr];
    
    
    self.showScrollV.contentSize = CGSizeMake(SCREEN_WIDTH * array.count, SCREEN_HEIGHT/3.5);
    
    for (int i = 0; i < array.count; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3.5)];
        
        imgView.tag = 1000+i;
        imgView.userInteractionEnabled = YES;//打开交互
        
        NSURL *imgUrl = [array objectAtIndex:i];
        
        [imgView sd_setImageWithURL:imgUrl placeholderImage:PLACEHOLDER_IMAGE];
        
        [self.showScrollV addSubview:imgView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [imgView addGestureRecognizer:tap];
        
     }
    
    }
    
}



-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (self.model.ads.count == 0) {
        if (self.model.photosetID != nil) {
            [self.headerImgDeleage presentShowsDataToController:self.model.photosetID :self.model.replyCount];

        }else{
            [self.headerImgDeleage presentShowsDataToController:self.model.docid :self.model.replyCount];
        }
    }else{
   NSString *url = [self.urlIdArr objectAtIndex:tap.view.tag - 1000];
    
       
    [self.headerImgDeleage presentShowsDataToController:url :self.model.replyCount];
    }
}




#pragma mark -
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
